﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.OracleClient;
using System.Text;

namespace Xiaov.SmartWeb.Application.TZ.XB
{
  public partial class XBTZQS : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string rq1 = Request.QueryString["rq1"].ToString();
      string rq2 = Request.QueryString["rq2"].ToString();
      StringBuilder sb = new StringBuilder();
      sb.Append("{");
      sb.Append("\"kzks\":" + kzks(rq1, rq2) + ",");
      string wjks = wzks(rq1, rq2);
      sb.Append("\"wcjks\":" + wjks + ",");
      sb.Append("\"zjjc\":" + zjjc(rq1, rq2) + ",");
      sb.Append("\"wcjjc\":" + wzjc(rq1, rq2) + ",");
      sb.Append("\"wcjjs\":0,");
      sb.Append("\"wcjpjjs\":0,");
      sb.Append("\"zjty\":" + zjty(rq1, rq2) + ",");
      sb.Append("\"zjysd\":" + zjysd(rq1, rq2) + ",");
      sb.Append("\"jxzs\":" + jxzs(rq1, rq2) + ",");
      string cs = wcjjjzq(rq1, rq2);
      sb.Append("\"wcjjjzq\":" + cs + ",");
      sb.Append("\"wcjpjjjzq\":" + wcjpjjjzq(cs, wjks) + ",");
      cs = qzzq(rq1, rq2);
      sb.Append("\"qzzq\":" + cs + ",");
      sb.Append("\"pjqzzq\":" + pjqzzq(cs, wjks) + ",");
      cs = zjzq(rq1, rq2);
      sb.Append("\"zjzq\":" + cs + ",");
      sb.Append("\"pjzjzq\":" + pjzjzq(cs, wjks) + ",");
      cs = wjzq(rq1, rq2);
      sb.Append("\"wjzq\":" + cs + ",");
      sb.Append("\"pjjjzq\":" + pjwjzq(cs, wjks));
      sb.Append("}");
      Response.Write(sb.ToString());
      Response.ContentType = "text/plain";
      Response.End();
    }

    //开钻口数
    protected string kzks(string rq1, string rq2)
    {
      string s = "0";
      string sql = "select count(jh) jh from tz_xbzjsj where ljjc=brjc and brjc<>0 and rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //完钻口数
    protected string wzks(string rq1, string rq2)
    {
      string s = "0";
      string sql = "select count(distinct substr(jh,1,length(jh)-3))  from tz_xbzjsj t where jh like '%待井位' and rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //钻井进尺
    protected string zjjc(string rq1, string rq2)
    {
      string s = "0";
      string sql = "select sum(brjc) jh from tz_xbzjsj where rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //完钻进尺
    protected string wzjc(string rq1, string rq2)
    {
      string s = "0";
      string sql = @"select sum(brjc) from tz_xbzjsj a, 
  (select distinct substr(jh,1,length(jh)-3) jh from tz_xbzjsj t where jh like '%待井位') b
where a.jh=b.jh and rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //钻机台月
    protected string zjty(string rq1, string rq2)
    {
      string s = "0";
      string sql = "select round(sum(zjgzzsj)/720,2) from tz_xbzjsj where rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //钻机月速度
    protected string zjysd(string rq1, string rq2)
    {
      string s = "0";
      string sql = "select round(sum(brjc)/sum(zjgzzsj)/720,2) from tz_xbzjsj where rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //机械钻速
    protected string jxzs(string rq1, string rq2)
    {
      string s = "0";
      string sql = "select round(sum(brjc)/sum(czsj)/720,2) from tz_xbzjsj where rq>=:0 and rq<=:1";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //完成井建井周期
    protected string wcjjjzq(string rq1, string rq2)
    {
      string s = "0";
      string sql = @"select sum(a.jjzqsj) from tz_xbzjsj a where a.jh in 
    (select distinct substr(jh,1,length(jh)-3) jh from tz_xbzjsj b 
    where jh like '%待井位' and  rq>=:0 and rq<=:1) "; ;
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //完成井平均建井周期
    protected string wcjpjjjzq(string qzzq, string wjks)
    {
      string s = "0";
      if (wjks == "0")
        s += "0";
      else
        s = Math.Round((double.Parse(qzzq) + double.Parse(wjks)), 2).ToString();
      return s;
    }

    //迁装周期
    protected string qzzq(string rq1, string rq2)
    {
      string s = "";
      string sql = @"select sum(a.basj) from tz_xbzjsj a where a.jh in 
    (select distinct substr(jh,1,length(jh)-3) jh from tz_xbzjsj b 
    where jh like '%待井位' and  rq>=:0 and rq<=:1) "; ;
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //平均迁装周期
    protected string pjqzzq(string qzzq, string wjks)
    {
      string s = "0";
      if (wjks == "0")
        s = "0";
      else
        s = Math.Round((double.Parse(qzzq) + double.Parse(wjks)), 2).ToString();
      return s;
    }

    //钻井周期
    protected string zjzq(string rq1, string rq2)
    {
      string s = "0";
      string sql = @"select sum(zjgzzsj) from tz_xbzjsj a where 
    ljjc<=(select max(ljjc) from tz_xbzjsj where a.jh=jh group by jh) and brjc<>0
    and a.jh in 
    (select distinct substr(jh,1,length(jh)-3) jh from tz_xbzjsj b 
    where jh like '%待井位' and rq>=:0 and rq<=:1) ";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //平均钻井周期
    protected string pjzjzq(string zjzq, string wjks)
    {
      string s = "0";
      if (wjks == "0")
        s = "0";
      else
        s = Math.Round((double.Parse(zjzq) + double.Parse(wjks)), 2).ToString();
      return s;
    }

    //完井周期
    protected string wjzq(string rq1, string rq2)
    {
      string s = "0";
      string sql = @"select sum(a.zjgzzsj) from tz_xbzjsj a where a.jh in 
    (select distinct substr(jh,1,length(jh)-3) jh from tz_xbzjsj b 
    where jh like '%待井位' and  rq>=:0 and rq<=:1) ";
      OracleParameter[] paras = new OracleParameter[]{
					new OracleParameter(":0", OracleType.VarChar,30),
					new OracleParameter(":1", OracleType.VarChar,30)};
      paras[0].Value = rq1;
      paras[1].Value = rq2;
      s = DbHelperOra.GetSingle(sql, paras).ToString();
      return s;
    }

    //平均完井周期
    protected string pjwjzq(string wjzq, string wjks)
    {
      string s = "0";
      if (wjks == "0")
        s = "0";
      else
        s = Math.Round((double.Parse(wjzq) + double.Parse(wjks)), 2).ToString();
      return s;
    }
  }
}